<?php

/**
 * AddThisWidget displays the social/sharing button(s).
 *
 * To use this widget, you may insert the following code in a view:
 *
 * <pre>
 * $this->widget('AddThisWidget',array(
 * 'config'=>array(
 *   'ui_click'=>true,
 * ),
 * 'share'=>array(
 *       'url'=>'http://www.vinatraders.com/'
 * ),
 * 'toolBoxCssClass'=>'',
 * 'services'=>array(
 *       'facebook'=>array('addthis:url'=>'http://www.facebook.com/thuaniser.com.vn',),
 *        'twitter'=>array(),
 *        'google_plusone'=>array(),
 *    ),
 * 'defaultButtons'=>array(
 *        'class'=>'addthis_button_compact',
 *         'addthis:url'=>'http://www.facebook.com/thuaniser.com.vn',
 *   ),
 * ));
 * </pre>
 * If you use ajax to re-render the button, you may need to re-init the buttons, here is how (put them 
 * right inside ajax.success function):
 * <pre>
 * window.addthis.ost = 0;
 * window.addthis.ready();
 * </pre>
 * 
 *For full and most ecxactly configs options or sharing options, please reference here :
 * {@link: https://www.addthis.com/help/client-api  AddThis Client API} documentation
 * for possible options (name-value pairs).
 *
 * @author Brian Nguyen <thuan.nguyen@webflexica.com>
 * @copyright Copyright &copy; 2011-2012 Flexica Solutions. Co.ltd
 * @since: 1.0
 */
class AddThisWidget extends CWidget
{
	/**
	 * Id of the toolbox
	 *
	 * @var mixed
	 */
	public $id='';
	/**
	 * Profile id connect to Addthis API.
	 *
	 * @var mixed
	 */
	public $pubid='ra-4e7c57511bb76da5';

	/**
	 * UI Configuration Options
	 * The same object format can be used for specifying global or instance configuration.
	 * To specify global configuration, the special name addthis_config must be used.
	 *
	 * For full and most ecxactly key-value pairs, you guys can visit here :
	 * @link: https://www.addthis.com/help/client-api#configuration-ui
	 *
	 * @var mixed
	 */
	public $config=array();

	/**
	 * Sharing Configuration Options
	 * The same object format can be used for specifying global or instance configuration.
	 * To specify global configuration, the special name addthis_share must be used.
	 *
	 * Templates
	 * Some destinations let users post arbitrary content. By using a template, you can customize the
	 * default post.
	 *         <Note: Support for templates in Twitter will be deprecated soon in favor of HTML attributes>
	 * Sample template:
	 * twitter: 'check out {{url}} (from @addthis)'
	 * We'll fill in a few different tokens for you -- you can use all, some or none of them:
	 *
	 * {{url}}
	 *the URL being shared; on destinations that have a length limit (i.e., Twitter), we'll automatically shorten the URL for you
	 *{{lurl}}
	 *the URL being shared; even on destinations that have a length limit, this token (short for "long URL") will never shorten your URL
	 *{{title}}
	 *the title being shared
	 *{{html}}
	 *the arbitrary HTML shared (the "html" parameter above)
	 *
	 *You can provide one template per destination that supports them:
	 *
	 *var addthis_share =
	 *{
	 * // ...
	 * templates: {
	 *   twitter: 'check out {{url}} (from @example_dot_com)'
	 * }
	 * }
	 *
	 * For full and most ecxactly key-value pairs, you guys can visit here :
	 * @link :https://www.addthis.com/help/client-api#configuration-sharing
	 * @var mixed
	 */
	public $share=array();

	/**
	 * You can render the AddThis button anywhere on your page using JavaScript. All of our JavaScript
	 * rendering methods take the form:
	 *     addthis.method(target(s), [configurationObject], [sharingObject]);
	 *
	 * @var mixed
	 */
	public $method='toolbox';

	/**
	 * the toolbox Css Class
	 *  For full and most ecxactly key-value pairs, you guys can visit here :
	 * @link: https://www.addthis.com/help/client-api#rendering-decoration
	 *
	 * @var mixed
	 */
	public $toolBoxCssClass='';

	/**
	 * Defaut button config, if you don't want to display the button, set 
     * <pre>
     * 'defaultButtons'=>false,
     * </pre>
	 *
	 'defaultButtons'=>array(
	 'class'=>'addthis_button',
	 'image'=>array(
	 'src'=>'http://s7.addthis.com/static/btn/v2/lg-share-en.gif',
	 ),
	 'text'=>'Share',
	 );
	 * @var mixed
	 */
	public $defaultButtons=array(
			'class'=>'addthis_button',
			'image'=>array(
					'src'=>'http://s7.addthis.com/static/btn/v2/lg-share-en.gif',
			),
	);
    /**
    * To enable asynchronous loading, add the querystring parameter "async" to the end of 
    * the addthis_widget.js script tag. Here's an example
    * <pre>
    * <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#async=1"></script>
    * </pre>
    * 
    * @var mixed
    */
	public $async=0;

	/**
	 * This is how to config service list ,
	 *
	 * For the full list of service, you guys can visit here:
	 * @link: http://www.addthis.com/services/list#.Tw8BgaX9O8A
	 *
	 * @Warning : each service must config with a pair of name and config, default config must be an array (nullable).
	 * For example:
	 * 'service'=>array(
	 * 'facebook'=>array(),
	 * 'twitter'=>array(),
	 * //'facebook', ===> wrong signature
	 * )
	 *
	 * @var array of service
	 */
	public $services=array();

	/**
	 * Wrapper htmlOptions
	 *
	 * @var mixed
	 */
	public $htmlOptions=array();

	/**
	 * Initializing the Addthis core script.
	 *
	 */
	public function init(){
		$this->registerAddThisCore();
		parent::init();
	}
	/**
	 * Render all content.
	 *
	 */
	public function run(){
		$this->renderConfigOptions();
		$this->renderShareOptions();
		$this->renderAddThis();
	}
	/**
	 * Register AddThis Core
	 *
	 */
	public function registerAddThisCore(){
		$cs=Yii::app()->getClientScript();
		$addThis='http://s7.addthis.com/js/250/addthis_widget.js';
		$src=($this->async!=1)?$addThis:$addThis.'#async='.$this->async;
        if($this->async==1)
            $cs->registerScript('#addThisInit', 'addthis.init();', CClientScript::POS_END); 
		$cs->registerScriptFile($src,CClientScript::POS_HEAD);
	}

	/**
	 * Render global var addthis_config value
	 *
	 */
	public function renderConfigOptions(){
		$cs=Yii::app()->getClientScript();
		$id=$this->id;
		if(empty($id)){
			if(!empty($this->config)){
				$config=CJavaScript::encode($this->config);
				$cs->registerScript('#addthis_config',"var addthis_config=$config;",CClientScript::POS_BEGIN);
			}
		}
	}

	/**
	 * * Render global var addthis_share value
	 *
	 */
	public function renderShareOptions(){
		$cs=Yii::app()->getClientScript();
		$id=$this->id;
		if(empty($id)){
			if(!empty($this->share)){
				$share=CJavaScript::encode($this->share);
				$cs->registerScript('#addthis_share',"var addthis_share=$share;",CClientScript::POS_BEGIN);
			}
		}
	}

	/**
	 * Render the default value
	 *
	 */
	public function renderDefaultButton(){

		if($this->defaultButtons!==false){
			if(empty($this->defaultButtons['class']))
				$this->defaultButtons['class']='addthis_button';
			$htmlOptions=$this->defaultButtons;
			unset($htmlOptions['text']);
			unset($htmlOptions['image']);
			echo CHtml::openTag('a',$htmlOptions);
			if(!empty($this->defaultButtons['image']))
			{
				echo CHtml::openTag('img',$this->defaultButtons['image']);
				echo CHtml::closeTag('img');
			}
			if(!empty($this->defaultButtons['text'])){
				echo "<span>{$this->defaultButtons['text']}</span>";
			}
			echo CHtml::closeTag('a');
		}
	}

	/**
	 * Render all service buttons in config service.
	 *
	 */
	public function renderServiceButtons(){
		if(!empty($this->services)){
			$services=$this->services;
			foreach ($services as $name => $config){
				if(empty($name)) continue;
				if(empty($config) || empty($config['class']))
					$config['class']='addthis_button_'.strtolower($name);
				echo CHtml::openTag('a',$config);
				echo CHtml::closeTag('a');
			}
		}
	}
	/**
	 * Render whole Addthis buttons
	 *
	 */
	public function renderAddThis(){
		$cs=Yii::app()->getClientScript();
		$id=$this->id;
		if (isset($this->htmlOptions['id']))
			$id = $this->htmlOptions['id'];
		else
			$this->htmlOptions['id']=$id;
		$cssClass=$this->toolBoxCssClass;
		if(isset($this->htmlOptions['class']))
			$cssClass=$this->htmlOptions['class'];
		else $this->htmlOptions['class']=$cssClass;
		$this->htmlOptions['class']='addthis_toolbox addthis_default_style '.$cssClass;
		echo CHtml::openTag('div',$this->htmlOptions);
		$this->renderServiceButtons();
		$this->renderDefaultButton();
		echo CHtml::closeTag('div');
		if(!empty($id)){
			$config=CJavaScript::encode($this->config);
			$share=CJavaScript::encode($this->share);
			$script="addthis.toolbox('#$id', $config, $share);";
			$cs->registerScript("#button_$id",$script,CClientScript::POS_END);
		}
	}

}
?>

